﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;


public class ChooseTheBestOne
{
    public int countNumber(int N)
    {
        List<int> employeeList = new List<int>();
        for (int i = 1; i <= N; i++)
        {
            employeeList.Add(i);
        }

        int prevPos = 0;
        for (int i = 1; i <= N; i++)
        {
            double pos = Math.Pow(i, 3);
            int rpos = (int)((pos - 1 + prevPos) % employeeList.Count);
            employeeList.RemoveAt(rpos);

            prevPos = rpos;
            if (employeeList.Count == 1) return employeeList[0];
        }

        return 1;
    }
}

public class GoodCompanyDivTwo
{
    public int countGood(int[] superior, int[] workType)
    {
        SortedList<int, List<int>> departments = new SortedList<int, List<int>>();

        for (int i = 0; i < superior.Length; i++)
        {
            departments[i] = new List<int>();
            departments[i].Add(i);
            for (int j = i; j < superior.Length; j++)
            {
                int boss = superior[j];
                if (boss == i) departments[i].Add(j);
            }
        }

        int diverse = 0;
        foreach (var kv in departments)
        {
            int boss = kv.Key;
            SortedList<int, bool> workList = new SortedList<int, bool>();
            foreach (int i in kv.Value)
            {
                int work = workType[i];
                workList[work] = true;
            }
            if (workList.Count == kv.Value.Count)
            {
                diverse++;
            }
        }

        return diverse;
    }
}
